package org.jprobe.utils;

import java.io.File;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
/**
 *
 * ANT builder
 *
 * @author Hussain Al-Mutawa
 * @version 1.0
 * @since 1.0
 */
public class AntBuilder extends DefaultLogger{
	private transient boolean failed;
	/**
	 * initializes new ANT builder and makes it ready to compile the project
	 * chosen as the argument
	 *
	 * @param buildFile the build directory of the project to be compiled
	 */
	public AntBuilder(final File buildFile) {
		super();

		//File buildFile = new File("build.xml");
		final Project project = new Project();
		project.setUserProperty("ant.file", buildFile.getAbsolutePath());

		this.setErrorPrintStream(System.err);
		this.setOutputPrintStream(System.out);
		this.setMessageOutputLevel(Project.MSG_INFO);
		project.addBuildListener(this);

		try {
			project.fireBuildStarted();
			project.init();
			final ProjectHelper helper = ProjectHelper.getProjectHelper();
			project.addReference("ant.projectHelper", helper);
			helper.parse(project, buildFile);
			project.executeTarget(project.getDefaultTarget());
			project.fireBuildFinished(null);
		} catch (final BuildException e) {
			failed=true;
			project.fireBuildFinished(e);
		}
	}
	public boolean isFailed() {
		return failed;
	}
}
